package com.syqc.monitor.ui.carinfo

/**
 *@author  hfjs
 *@time   2024/9/27 15:23
 *@desc
 */

import android.view.Gravity
import android.view.View
import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels
import com.syqc.comlib.GuideControl.isBaseCar
import com.syqc.comlib.base.LazyFragment
import com.syqc.comlib.utils.copyText
import com.syqc.entity.GpsDetail
import com.syqc.monitor.R
import com.syqc.monitor.databinding.FragmentCarBaseOldBinding
import com.syqc.monitor.popup.DriversPopup
import com.syqc.utils.ToolsUtil
import com.syqc.utils.Units


/** @Author Jasper @Time 2021/2/27 @Desc 定位基础信息 */
class CarBaseFragmentOld : LazyFragment<FragmentCarBaseOldBinding>(FragmentCarBaseOldBinding::inflate) {

    private var firstPhone = ""
    private var ownerPhone: String? = ""
    private val mViewModel by activityViewModels<CarInfoViewModel>()
    private var mCarPopup: CarBasePopup? = null

    private val mDriversPopup by lazy { DriversPopup(requireContext()) }
    override fun initView(view: View) {
        if (isBaseCar) {
            mCarPopup = CarBasePopup(requireContext())
            mCarPopup?.showAtLocation(binding.root, Gravity.NO_GRAVITY, 0, 0)
            isBaseCar = false
        }
    }

    override fun initData() {
        mViewModel.getGprR()
        mViewModel.locationLive.observe(this) {
            handleMsg(it)
        }
    }

    override fun initEvent() {
        binding.ivPhone.setOnClickListener {
            ToolsUtil.talkPhone(requireContext(), firstPhone)
        }

        binding.ivNext.setOnClickListener {
            if (mViewModel.driverInfo.isNotEmpty())
                mDriversPopup.show(requireActivity().window.decorView, mViewModel.driverInfo)
        }

        binding.tvAddress.setOnLongClickListener {
            copyText(
                "${getString(R.string.desc_com_address)} : ${binding.tvAddress.text}",
                requireContext()
            )
            return@setOnLongClickListener true
        }
        binding.ivOwnerPhone.setOnClickListener {
            ToolsUtil.talkPhone(requireContext(), ownerPhone)
        }
    }

    private fun handleMsg(entity: GpsDetail) {
        binding.lrTvPlateInfo.setRightText("${entity.carPlate}  ${entity.plateColor ?: ""}")
        binding.lrTvCarName.setRightText(entity.carName)

        binding.lrTvServiceTime.setRightText(
            if (entity.expTime.isNullOrEmpty()) getString(R.string.desc_no_info) else entity.expTime
        )

        binding.lrTvCarTime.setRightText(entity.time)
        binding.lrTvCarACC.setRightText(entity.accTime)
        binding.lrTvCarState.setRightText("${entity.speed} ${Units.speed},${entity.drctCn} | ${entity.stateCn}")
        binding.lrTvOwner.setRightText(entity.owner ?: "")
        ownerPhone = entity.ownerPhone
        binding.tvOwnerPhone.text = ownerPhone ?: ""
        binding.ivOwnerPhone.isVisible = !ownerPhone.isNullOrEmpty()

        binding.lrTvCarTeam.setRightText(entity.teamName)

        binding.lrTvCarRun.setRightText(entity.runStopTime)
        binding.lrTvCarTotalMile.setRightText("${entity.mile} ${Units.km}")
        binding.lrTvCarDayMile.setRightText("${entity.preMile} ${Units.km}")
        binding.tvAddress.text = entity.addr ?: ""

        binding.lrTvCarComSignal.setRightText(entity.sgn)
        binding.lrTvCarSim.setRightText(entity.simCard)
        binding.lrTvCarLocation.setRightText(entity.satl)
        binding.lrTvCarTmnNum.setRightText(entity.tmnNo)
        binding.lrTvCarTmnKey.setRightText(entity.tmnKey ?: "")

        binding.lrTvCarTmnType.setRightText(entity.tmnType ?: "")
        binding.lrTvCarTmnTime.setRightText(entity.tmnExpirTime ?: "")

        binding.lrTvCarTmnbus.setRightText(entity.tmnMakerModel ?: "")
        binding.lrTvCarSetMeal.setRightText(entity.tmnSimMeal ?: "")
        binding.lrTvCarSimType.setRightText(entity.tmnSimType ?: "")
        binding.lrTvCarDesc.setRightText(entity.des ?: "")

        if (mViewModel.driverInfo.isNotEmpty()) {
            val driverInfo = mViewModel.driverInfo
            firstPhone = if (driverInfo.isEmpty()) "" else driverInfo[0].driverPhone ?: ""
            binding.tvDriverInfo.text =
                if (driverInfo.isEmpty()) "" else "${driverInfo[0].driverName}  $firstPhone"
            binding.ivPhone.isVisible = firstPhone.isNotEmpty()
            binding.ivNext.isVisible = mViewModel.driverInfo.size > 1
        } else {
            firstPhone = entity.drvPhone
            binding.tvDriverInfo.text =
                entity.drvName.ifEmpty { requireContext().getString(R.string.desc_no_info) }
            binding.ivPhone.isVisible = firstPhone.isNotEmpty()
            binding.ivNext.isVisible = false
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        if (mCarPopup != null && mCarPopup?.isShowing == true) {
            mCarPopup?.dismiss()
            mCarPopup = null
        }
    }
}